import request from "@/axios"
import store from "@/store"
function initArticle(article){
  
  if(article.articleState == 0)  article.tag ={label:'审核中',style:{  '--Color':'#E6A23C'}}
  else if(article.classify == 1) article.tag ={label:'图文',style:{  '--Color':'#66B1FF'}}
  else if(article.classify == 2) article.tag ={label:'视频',style:{  '--Color':'#66B1FF'}}
  else article.tag ={label:'MD',style:{  '--Color':'#66B1FF'}}

  article.type =  store.state.Init.ArticleType?.find(item => item.id = type)?.name

  return article
}
export default ({
  namespaced:true,
  state: {
    //个人主页的数据
    PersonalData:null,

    //导航index
    NaviIndex:1,

    //获取数据状态  1:加载中 2:正常的搜索结果  3:空数据  4:网络问题或者其他问题
    DataState:1,

    //获取数据
    Data:null,
    DataInfo:{pageNo:1,pageSize:5,total:0,length:0,type:'space'},

    //修改用户信息窗口
    EditInformationState:false,
    //修改用户密码窗口
    EditPasswordState:false,

    //用户申请好友窗口
    ApplyFriendState:false,

    //回收站窗口
    RecycleBinState:false,



    //搜索文章窗口
    SearchArticleState:false,

     //浏览量窗口
     BrowseState:false,
     BrowseData:[],
     BrowseType:'viewed',

    //空间设置状态
    SpaceSetState:false,

  },
  getters: {},
  mutations: {
      //开启 关闭 修改用户信息窗口
      EDITINFORMATIONON:state => state.EditInformationState=true,
      EDITINFORMATIONDOWN:state => state.EditInformationState=false,

      //开启 关闭  修改用户信息窗口
      EDITPASSWORDON:state => state.EditPasswordState=true,
      EDITPASSWORDDOWN:state => state.EditPasswordState=false,
 
      //开启 关闭   搜索文章窗口
      SEARCHARTICLEON:state => state.SearchArticleState=true,
      SEARCHARTICLEDOWN:state => state.SearchArticleState=false,

      //开启 关闭 回收站窗口
      RECYCLEBINON:state => state.RecycleBinState=true,
      RECYCLEBINDOWN:state => state.RecycleBinState=false,

        //开启 关闭 用户申请好友窗口
        APPLYFRIENDON:state => state.ApplyFriendState=true,
        APPLYFRIENDDOWN:state => state.ApplyFriendState=false,
      
       /*浏览*/
       BROWSEON:state => state.BrowseState=true,  //开启 浏览量窗口
       BROWSEDOWN:state => state.BrowseState=false,//开启  浏览量窗口
       SETBROWSETYPE:(state,type) => state.BrowseType = type, //浏览类型
       SETBROWSEDATA:(state,data) => state.BrowseData=data , // 设置访客记录
     
      //开启 关闭  空间设置状态
      SPACESETON:state => state.SpaceSetState=true,
      SPACESETDOWN:state => state.SpaceSetState=false,
     
     //设置个人数据
     SETPERSONAL:(state,data) =>{
        state.PersonalData=data
        state.NaviIndex=1
        state.DataState=1
        state.DataInfo={pageNo:1,pageSize:5,total:0,length:0,type:'space'}
     },
     SETPERSONALDATA:(state,data) => state.PersonalData = data ,
     
     //设置index
     SETNAVIINDEX:(state,{index,type}) =>{
        state.NaviIndex=index
        state.DataState=1
        state.DataInfo={pageNo:1,pageSize:5,total:0,length:0,type}

     },

     //设置数据状态
      SETDATASTATE:(state,index)=> state.DataState=index,

      //设置数据
      SETDATA:(state,{data,index})=>{
        setTimeout(() => {
            if(state.NaviIndex==index || index == null)
            {  
                  state.Data=data.records
                  state.DataInfo.total = data.total
                  state.DataInfo.length = state.Data.length

                  if(data.total==0)
                  state.DataState=3
                  else  state.DataState=2
                  state.Data.forEach(item =>{
                    item = initArticle(item)
                  })
            }
      }, 200)

      //
    }
  },
  actions:{
    //获取主页用户信息
    async getPersonalData(context,data){
        let res=await request.get("/api/userBrowse/homepage",{params:{
          homepageuid:data.homepageuid,
          uid:data.uid
        }}) 
       if(res.data.code==200)
          context.commit('SETPERSONAL',res.data.data)
       return res.data
    },
    //判断是否可以修改
    judge(context,index){
      console.log(context.state.NaviIndex,index)
      return context.state.NaviIndex==index
    },

    //获取发布的文章
    async getArticle(context,{uid,index}) {
       
    
      let {pageNo,pageSize,type} = context.state.DataInfo
      context.commit('SETDATASTATE',1)
      try{
        let res= await  request.get('/api/article/get',{params:{createdUid:uid,type,pageNo,pageSize}})	 
        if(res.data.code==200) context.commit('SETDATA',{data:res.data.data,index})   
        else if(res.data.code == 403)   context.commit('SETDATASTATE',5)    
        else context.commit('SETDATASTATE',4)
        
      }catch(error){ if(context.state.NaviIndex==1) context.commit('SETDATASTATE',4) }
    },

    //获取访客记录
    async getBrowseData(context,uid){
       let res = await request.get('/api/userBrowse/get',{params:{uid}})
       if(res.data.code == 200) context.commit('SETBROWSEDATA',res.data.data)

    }


    //
  },
})